using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.WowAddonStudio.Lua.RuntimeService.ShellHost;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.Common
{
	/// <summary>
	/// Represents errors that occur during LuaRuntime execution.
	/// </summary>
	[Serializable]
	public class RuntimeShellHostException : LuaRuntimeException
	{
		public IShellHost ShellHost { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="RuntimeShellHostException"/> class.
		/// </summary>
		/// <param name="runtime">The <see cref="LuaRuntime"/> parameter.</param>
		public RuntimeShellHostException(ILuaRuntime runtime)
			: this(runtime, null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="RuntimeShellHostException"/> class
		///  with a specified error message.
		/// </summary>
		/// <param name="runtime">The <see cref="LuaRuntime"/> parameter.</param>
		/// <param name="message">The message that describes the error.</param>
		public RuntimeShellHostException(ILuaRuntime runtime, string message)
			: this(runtime, message, null)
		{

		}


		/// <summary>
		/// Initializes a new instance of the <see cref="RuntimeShellHostException"/> class with a specified error message
		/// and a reference to the inner exception that is the cause of this exception.
		/// </summary>
		/// <param name="runtime">The <see cref="LuaRuntime"/> parameter.</param>
		/// <param name="message">The message that describes the error.</param>
		/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
		public RuntimeShellHostException(ILuaRuntime runtime, string message, Exception innerException)
			: base(runtime, message, innerException)
		{
			if (runtime != null)
				ShellHost = runtime.RuntimeShellHost;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="RuntimeShellHostException"/> class with serialized data.
		/// </summary>
		/// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
		protected RuntimeShellHostException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			if (info != null)
				ShellHost = info.GetValue("ShellHost", typeof (IShellHost)) as IShellHost;
		}

		/// <summary>
		/// When overridden in a derived class, sets the <see cref="System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
		/// </summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
		[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("ShellHost", ShellHost);
		}

	}
}